package com.hengfoxwolf.kiuilp
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class DataItemDecoration(space: Int, column: Int) : RecyclerView.ItemDecoration() {

    private val mSpace = space
    private val mColumn = column

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)

        //val itemCount = parent.childCount
        val index = parent.getChildAdapterPosition(view)

        when (index % mColumn) {
            0 -> {
                val top = if (index == 0) 0 else mSpace
                outRect.set(mSpace / 2, top, mSpace / 2, 0)
            }
            mColumn - 1 -> {
                val top = if (index == (mColumn - 1)) 0 else mSpace
                outRect.set(mSpace / 2, top, mSpace / 2, 0)
            }
            else -> {
                val top = if (index < (mColumn - 1)) 0 else mSpace
                outRect.set(mSpace / 2, top, mSpace / 2, 0)
            }
        }
    }
}